<template>
  <div class="form"
    style="height:100%">
    <el-form :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="140px"
      size="small"
      style=" display: flex;
       flex-direction: column;
       align-items: center;
       justify-content: center;
       height:100%"
      class="demo-ruleForm">
      <el-col :span="8" style="line-height: 100px;
         font-size: 20px;
         font-weight: 600;">
               登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户名"
          prop="name">
          <el-input type="name"
            v-model="ruleForm.name"
            autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="密码"
          prop="pass">
          <el-input type="password"
            v-model="ruleForm.pass"
            autocomplete="off"></el-input>
        </el-form-item>
      </el-col>
      <el-form-item>
        <el-button @click="$router.push('/')">没注册，去注册</el-button>
        <el-button type="primary"
          @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "index",
  data () {
    var validatePass = (rule, value, callback) => {
      let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$/
      if (!reg.test(value)) {
        callback(new Error('密码必须是由4-20位字母+数字组合'))
      } else {
        callback()
      }
    };
    return {
      ruleForm: {
        name: '',
        pass: '',
      },
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>